package com.cloud.yangfeiself.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

/**
 *  springboot 上下文工具类
 *
 * @author Yang Fei
 * @date 2021/2/24
 */
@Configuration
public class ApplicationUtil implements ApplicationContextAware {

    public static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationUtil.applicationContext=applicationContext;
    }


    public static Object getBean(String name) {
        return applicationContext == null ? null : applicationContext.getBean(name);
    }

    public static <T> T getBean(Class<T> requiredType) {
        return applicationContext == null ? null : applicationContext.getBean(requiredType);
    }
}
